package com.anxinr.service.impl;

import com.anxinr.mapper.ShopMapper;
import com.anxinr.pojo.base.QueryBase;
import com.anxinr.pojo.dto.ShopDTO;
import com.anxinr.pojo.entity.Shop;
import com.anxinr.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author anxinr
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper ;
    /**
     * @author anxinr
     * @param shop
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageInfo<ShopDTO> queryPageShop(QueryBase<Shop> shop) {
        /**分页参数*/
        PageHelper.startPage(shop.getCurrentPage(),shop.getSize());
        List<Shop> shops = shopMapper.getShopInfo(shop);
        PageInfo<Shop> pageInfo = new PageInfo(shops,shop.getSize());
        List<Shop> list = pageInfo.getList();
        /**Entity赋值到DTO*/
        List<ShopDTO> responseInfo = new ArrayList() ;
        for (Shop shopList : list) {
            ShopDTO shopDTO = new ShopDTO();
            BeanUtils.copyProperties(shopList,shopDTO);
            responseInfo.add(shopDTO) ;
        }
        /**实际返回的数据*/
        PageInfo<ShopDTO> info = new PageInfo<>();
        info.setList(responseInfo);
        return info ;
    }
    /**
     * 新增门店
     * @param shop
     */
    @Override
    public void saveShop(ShopDTO shop) {
        /**实体类数据转换*/
        Shop saveInfo = new Shop();
        BeanUtils.copyProperties(shop,saveInfo);
        shopMapper.insert(saveInfo) ;
    }
    /**
     * 门店修改
     * @param shop
     */
    @Override
    public void updateShop(ShopDTO shop) {
        /**实体类数据转换*/
        Shop updateInfo = new Shop();
        BeanUtils.copyProperties(shop,updateInfo);
        shopMapper.updateById(updateInfo);
    }
    /**
     * 门店删除
     * @param id
     */
    @Override
    public void deleteShop(Long id) {
        shopMapper.deleteShop(id);
    }
}
